package net.ck.hzq;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import Bean.Kd100Bean;
import Utils.CheckPhoneNumberUtils;
import model.ListviewLpgisticsinfomationAdapter;

public class LogisticsinformationActivity extends Activity implements View.OnClickListener {

    private TextView logisticsinformation_txt_nodate;
    private ImageView logisticsinformation_back;
    private ListView logisticsinformation_back_listview;
    List<Kd100Bean.DataBean> data;
    ListviewLpgisticsinfomationAdapter listviewLpgisticsinfomationAdapter;

    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case 0x123:
                    listviewLpgisticsinfomationAdapter = new ListviewLpgisticsinfomationAdapter(data, LogisticsinformationActivity.this);
                    logisticsinformation_back_listview.setAdapter(listviewLpgisticsinfomationAdapter);
                    listviewLpgisticsinfomationAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logisticsinformation);

        TobuyApplication.getmInstance().onActivityCreate(this);

        logisticsinformation_back_listview = (ListView) findViewById(R.id.logisticsinformation_back_listview);
        logisticsinformation_txt_nodate = (TextView) findViewById(R.id.logisticsinformation_txt_nodate);
        logisticsinformation_back = (ImageView) findViewById(R.id.logisticsinformation_back);
        logisticsinformation_back.setOnClickListener(this);

        new Thread() {
            @Override
            public void run() {
                super.run();
                String name = getIntent().getStringExtra("name");
                String id = getIntent().getStringExtra("id");
                String url = "http://www.kuaidi100.com/query?type=" + name + "&postid=" + id;

                try {

                    String s = CheckPhoneNumberUtils.getHtml(url);

                    if (s != null) {

                        JSONObject object = new JSONObject(s);//获取json数据
                        JSONArray jsonArray = object.getJSONArray("data");//获取数据集名称为data的数据
                        if (jsonArray.length() > 0) {
                            data = new ArrayList<>();
                            for (int i = 0; i < jsonArray.length(); i++) {
                                Kd100Bean.DataBean dataBean = new Kd100Bean.DataBean();
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                dataBean.setTime((String) jsonObject.get("time"));
                                dataBean.setContext((String) jsonObject.get("context"));
                                dataBean.setLocation((String) jsonObject.get("location"));
                                data.add(dataBean);
                            }

                            handler.sendEmptyMessage(0x123);

                        } else {
                            logisticsinformation_txt_nodate.setVisibility(View.VISIBLE);
                            logisticsinformation_back_listview.setVisibility(View.GONE);
                        }

                    }

                } catch (Exception e) {

                }

            }
        }.start();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.logisticsinformation_back:
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
